﻿
Shader "GJ/Test" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _SColor ("Second Color", Color) = (1,1,1,1)
    _MainTex ("Main Texture", 2D) = "black" {}
    _ColSpeed("ColSpeed", Range(0, 10)) = 0
    _ColWeight("ColWeight", Range(-1, 1)) = 0
    _ScaleSpeed("ScaleSpeed", Range(0, 10)) = 0
    _ScaleSize("ScaleSize", Range(-10, 10)) = 0
    _RotateSpeed("RotateSpeed", Range(0, 10)) = 0
    _RotateStartAngle("RotateStartAngle", Range(-360, 360)) = 0
    _RotateEndAngle("RotateEndAngle", Range(-360, 360)) = 0
}
SubShader {
    Tags {
        "Queue"="Transparent"
        "IgnoreProjector"="True"
        "RenderType"="Transparent"
    }
    Cull Off Lighting Off ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0

        #include "UnityCG.cginc"

        sampler2D _MainTex;
        fixed4 _Color;
        fixed4 _SColor;
        fixed _ColSpeed;
        fixed _ScaleSpeed;
        fixed _ColWeight;
        fixed _ScaleSize;
        fixed _RotateSpeed;
        fixed _RotateStartAngle;
        fixed _RotateEndAngle;

        struct appdata_t {
            float4 vertex : POSITION;
            fixed4 color : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        struct v2f {
            float4 vertex : SV_POSITION;
            fixed4 color : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_OUTPUT_STEREO
        };

        float4 _MainTex_ST;

        float2 Rotation(float2 uv,float angle) {
            // Pivot
            float2 pivot = float2(0.5, 0.5);
            // Rotation Matrix
            float cosAngle = cos(angle);
            float sinAngle = sin(angle);
            float2x2 rot = float2x2(cosAngle, -sinAngle, sinAngle, cosAngle);

            // Rotation consedering pivot
            uv = uv - pivot;
            uv = mul(rot, uv);
            uv += pivot;
            return uv;

	        float radian = radians(angle);
	        float _sin = sin(radian);
	        float _cos = cos(radian);
	        return float2((uv.x * _sin) - (uv.x * _cos), (uv.y * _sin) - (uv.y * _cos));
        }
        
        v2f vert (appdata_t v)
        {
            v2f o;
            float s;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.color = v.color;
            o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
            if (_RotateSpeed != 0 && _RotateStartAngle != _RotateEndAngle)
            {
                float r = abs(sin(_Time.y * _RotateSpeed));
                float angle = lerp(_RotateStartAngle, _RotateEndAngle, r);
                angle = angle / (360 / UNITY_TWO_PI);
                o.texcoord = Rotation(o.texcoord, angle);
            }
            if (_ScaleSpeed != 0 && _ScaleSize != 0)
            {
                float s = abs(sin(_Time.y * _ScaleSpeed)) * -1;
                o.texcoord = o.texcoord * (s * _ScaleSize + 1) + (s * _ScaleSize * 0.5 * -1);  
            }
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            fixed4 col = _Color * i.color;
            if (_ColSpeed != 0)
            {
                float t = abs(sin(_ColSpeed * _Time.y)) + _ColWeight;
                col = lerp(_Color * i.color, _SColor * i.color, t);
            }
            fixed4 tex = tex2D(_MainTex, i.texcoord);
            col = col * tex;
            return col;
        }
        ENDCG
    }
}

}